package com.ylsk.statistics.service.impl;
import com.google.common.collect.Maps;
import com.ylsk.common.core.domain.entity.SysDept;
import com.ylsk.statistics.mapper.BigScreenMapper;
import com.ylsk.statistics.service.BigScreenService;
import com.ylsk.system.mapper.SysDeptMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BigScreenServiceImpl implements BigScreenService {

    private static final Long PARENT_DEPT_ID = 101L;
    @Autowired
    private BigScreenMapper bigScreenMapper;

    @Autowired
    private SysDeptMapper deptMapper;


    @Override
    public List<Map<String, Object>> selectNumberTotal() {
        return bigScreenMapper.selectNumberTotal();
    }



    @Override
    public Map<String, Object> selectTodayCheckTotal() {
        Map<String,Object> retMap = Maps.newHashMap();
        List<SysDept> sysDeptList = getDept(PARENT_DEPT_ID);
        //应检查数量
        List<Integer> expectedData = Lists.newArrayList();
        //应检查数量
        List<Integer> actualData = Lists.newArrayList();
        //部门
        List<String> deptData = Lists.newArrayList();
        List<Map<String, Object>> mapList = bigScreenMapper.selectTodayCheckTotal();
        Map<Integer, List<Map<String, Object>>> groupMap = mapList.stream().collect(Collectors.groupingBy(item ->Integer.valueOf(item.get("checkType").toString())));
        List<Map<String, Object>> expectedDataList = groupMap.get(1);//应检查数量
        List<Map<String, Object>> actualDataList = groupMap.get(2);//实际检查数量

        Map<Long,Integer> expectedDataMap = Maps.newHashMap();
        Map<Long,Integer> actualDataMap = Maps.newHashMap();
        buildCheckData(expectedDataList,expectedDataMap);
        buildCheckData(actualDataList,actualDataMap);
        for (SysDept sysDept : sysDeptList) {
            final Long deptId = sysDept.getDeptId();
            expectedData.add(expectedDataMap.get(deptId) == null ? 0 : expectedDataMap.get(deptId));
            actualData.add(actualDataMap.get(deptId) == null ? 0 :actualDataMap.get(deptId));
            deptData.add(sysDept.getDeptName());
        }
        //应检查数量
        retMap.put("expectedData",expectedData);
        //实际检查数量
        retMap.put("actualData",actualData);
        retMap.put("deptData",deptData);
        return retMap;
    }

    @Override
    public List<Map<String, Object>> selectConstructionStatusTotal() {
        return bigScreenMapper.selectConstructionStatusTotal();
    }

    @Override
    public Map<String, Object> selectConstructionProblemTotal() {
        Map<String, Object> problemTotal = bigScreenMapper.selectConstructionProblemTotal();

        return problemTotal;
    }

    @Override
    public Map<String, Integer> selectCheckTotalByMonth() {
        List<Map<String, Object>> mapList = bigScreenMapper.selectCheckTotalByMonth();
        Map<Long, Integer> resultMap = new HashMap<>();
        for (Map<String, Object> map : mapList) {
            resultMap.put(Long.valueOf(map.get("deptId").toString()),Integer.valueOf(map.get("num").toString()));
        }
        List<SysDept> deptList = getDept(PARENT_DEPT_ID);
        Map<String,Integer> detpMap = new LinkedHashMap<>();
        for (SysDept sysDept : deptList) {
            final Long deptId = sysDept.getDeptId();
            detpMap.put(sysDept.getDeptName(),resultMap.get(deptId) == null ? 0 : resultMap.get(deptId));
        }
        return detpMap;
    }

    private List<SysDept> getDept(Long parentDeptId){
        SysDept sysDept = new SysDept();
        sysDept.setParentId(parentDeptId);
        return deptMapper.selectDeptList(sysDept);

    }

    private void buildCheckData(List<Map<String, Object>> mapList, Map<Long,Integer> dataMap){
        if(CollectionUtils.isEmpty(mapList)) return;
        for (Map<String, Object> map : mapList) {
            dataMap.put(Long.valueOf(map.get("deptId").toString()),Integer.valueOf(map.get("checkCount").toString()));
        }
    }
}
